package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.login.CustomLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumChannelVo;
import com.atguigu.tingshu.vo.search.AlbumDetailsVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    @Operation(summary = "商品上架")
    @GetMapping("/upper/album/{albumId}")
    public Result upperAlbum(@PathVariable("albumId") Long albumId) {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "商品下架")
    @GetMapping("down/album/{albumId}")
    public Result downAlbum(@PathVariable("albumId") Long albumId){
        this.searchService.downAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "首页检索")
    @PostMapping
    public Result<AlbumSearchResponseVo> albumSearch(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo responseVo = searchService.albumSearch(albumIndexQuery);
        return Result.ok(responseVo);
    }

    @CustomLogin(required = false)
    @Operation(summary = "专辑详情")
    @GetMapping("/{albumId}")
    public Result<AlbumDetailsVo> getAlbumDetails(@PathVariable("albumId") Long albumId) {
        AlbumDetailsVo resultVo = searchService.getAlbumDetails(albumId);
        return Result.ok(resultVo);
    }

    @Operation(summary = "查询频道页的热门数据")
    @GetMapping("channel/{category1Id}")
    public Result<List<AlbumChannelVo>> channel(@PathVariable Long category1Id) throws IOException {
        List<AlbumChannelVo> resultList = searchService.channel(category1Id);
        return Result.ok(resultList);
    }

    @Operation(summary = "专辑排行榜")
    @GetMapping("findRankingList/{category1Id}/{field}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(@PathVariable("category1Id") Long category1Id,
                                                          @PathVariable("field") String field) {
        List<AlbumInfoIndexVo> list = searchService.findRankingList(category1Id, field);
        return Result.ok(list);
    }

    @Operation(summary = "检索关键字补全")
    @GetMapping("/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable("keyword") String keyword) {
        List<String> resultList = searchService.completeSuggest(keyword);
        return Result.ok(resultList);
    }

    @Operation(summary = "更新最近的专辑统计信息到es")
    @GetMapping("updateLatelyAlbumStat")
    public Result updateLatelyAlbumStat(){
        this.searchService.updateLatelyAlbumStat();
        return Result.ok();
    }
}

